package cn.well.cloud.modular.service.impl.thread;

import cn.well.cloud.base.util.DateUtil;
import cn.well.cloud.base.wechat.api.impl.TemplateMessage;
import cn.well.cloud.base.wechat.model.NoticeInfo;
import cn.well.cloud.modular.model.CourseInfo;
import cn.well.cloud.modular.model.NoticeLog;
import cn.well.cloud.modular.model.UserInfo;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class WXTempUtils {


    /**
     *
     * @param ourseInfo 课程信息
     * @param userList 要发送的用户集合
     * @param token 微信调用凭证
     */
    public static void sendNotice(CourseInfo ourseInfo, List<UserInfo> userList,  String token,NoticeLog noticeLog){
        String tempId = noticeLog.getTemplateId();
        NoticeInfo noticeInfo = sendNoticeByOpenId(token, ourseInfo,tempId);
        ThreadPool pool = new ThreadPool();
//        createSendNotic(noticeInfo,userList,token);
        try {
            pool.createSendNoticePool(noticeInfo,userList,token, noticeLog);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 单线程循环形式发送
     * @param noticeInfo
     * @param userList
     * @param token
     */
    private static void createSendNotic(NoticeInfo noticeInfo,List<UserInfo> userList, String token){
        TemplateMessage templateMessage = new TemplateMessage();
        for(UserInfo u: userList){
            NoticeInfo newNoticeInfo = new NoticeInfo();
            newNoticeInfo = noticeInfo;
            newNoticeInfo.setTouser(u.getOpenId());
            Object object = null;
            try {
                object =  templateMessage.sendTempMessage(token, noticeInfo);
            } catch (IOException e) {
                log.error("发送失败");
                log.error(JSONObject.toJSONString(object));
                e.printStackTrace();
            }
        }
    }

    private static NoticeInfo sendNoticeByOpenId(String token, CourseInfo ourseInfo,String tempId){
        String courseUrl =  ourseInfo.getCourseUrl();
        String courseTime = DateUtil.formatMiddleDatetime( ourseInfo.getCourseTime());
        String courseName =  ourseInfo.getCourseName();
        String courseAddress =  ourseInfo.getCourseAddress();
        String courseRemark =  ourseInfo.getCourseRemark();

        NoticeInfo noticeInfo = new NoticeInfo();
        noticeInfo.setUrl(courseUrl);
        noticeInfo.setTemplate_id(tempId);
        Map<String,String> first = new HashMap<String,String>();
        first.put("value","直播即将开始");
        first.put("color","#173177");
        noticeInfo.setFirst(first);

        Map<String,String> keyword1 = new HashMap<String,String>();
        keyword1.put("value",courseTime);
        keyword1.put("color","#173177");
        noticeInfo.setKeyword1(keyword1);

        Map<String,String> keyword2 = new HashMap<String,String>();
        keyword2.put("value",courseName);
        keyword2.put("color","#173177");
        noticeInfo.setKeyword2(keyword2);

        Map<String,String> keyword3 = new HashMap<String,String>();
        keyword3.put("value",courseAddress);
        keyword3.put("color","#173177");
        noticeInfo.setKeyword3(keyword3);

        Map<String,String> remark = new HashMap<String,String>();
        remark.put("value",courseRemark);
        remark.put("color","#173177");
        noticeInfo.setRemark(remark);
        return noticeInfo;
    }


}
